package GUI.Show;

import java.awt.Color;
import java.util.Vector;

import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;

import data.Season;
import data.Show;
import GUI.SonnyTableModel;

public class ShowTableModel extends SonnyTableModel 
{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public ShowTableModel(String[] columnNames, Show show) 
	{
		super(columnNames, parseSeasonData(show));
		this.fireTableDataChanged();
	}

	public int findSelectedSeason(Show s)
	{
		int returnRow = -1;
		int rows = this.getRowCount();
		int selectedSeasonNumber = s.getSelectedSeason().getSeasonNumber();
		for(int i=0; i<rows; i++)
		{
			Object obj = this.getValueAt(i, 0);
			if(obj instanceof String)
			{
				String seasonString = (String)obj;
				seasonString = seasonString.replace("Season ", "");
				int seasonNumber = Integer.parseInt(seasonString);
				if(seasonNumber == selectedSeasonNumber)
				{
					returnRow = i;
					break;
				}
			}
		}
		return returnRow;
	}
	public static Object[][] parseSeasonData(Show s)
	{
		Vector<Season> seasons = s.getSeasons();
		String[][] returnString = new String[seasons.size()][1];
		for(int i=0; i<seasons.size(); i++)
		{
			int seasonNum = seasons.get(i).getSeasonNumber();
/*			if(s.getSelectedSeason().getSeasonNumber() == seasonNum)
			{
				returnString[i][0] = "*";
			}
			else
			{
				returnString[i][0] = " ";
			}
*/			
			returnString[i][0] = "Season " + seasonNum;
		}
		return returnString;
		
	}

	public void renderTable(JTable t, Show s) 
	{
		DefaultTableCellRenderer tcr = new DefaultTableCellRenderer();
		int row = this.findSelectedSeason(s);
		int col = 0;
		tcr.getTableCellRendererComponent(t, (Object)"Selected Season", false, false, row, col).setForeground(Color.GREEN);
	}

}
